<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();

	include_once APPPATH . "libraries/createfastlib/CF_Base.php";
	spl_autoload_register('CF_autoload');
//include_once "pdo_connect.php";
//include_once APPPATH . "libraries/createfastlib/db/CF_db_mysql.php";
	include_once APPPATH . "models/<cs_name>_model.php";
//parse_str($_SERVER['QUERY_STRING'], $_GET);


// Local Classes


// controller class

class <cs_name> extends CI_Controller {

	var $fieldspec;

	function __construct()
	{
		// load fieldspec
		include_once APPPATH . "fieldspecs/<cs_name>_spec.php";		
		$this->fieldspec = $tfrlist_spec;
		parent::__construct();
	}

	function act(){
		$mode = $_REQUEST['mode'];
		if($mode = 'delete'){
			$this->delete();
		}
	
	}	
	
	function add(){
		$this->load->helper('url');	
	
		include APPPATH . "fieldspecs/<cs_name>_spec.php";	
		$fs = new CF_FieldSpec($fieldspec);
		$ar_add_columns = $fs->GetColumnsArray('add');
		$newvalues = CF_ParamReciever::GetReqParamsByList($ar_add_columns,'',false);
		
		//$newvalues = array(0=>array('filterparam'=>'NEW'));
		$result = <cs_name>_mdl::do_add($keyvalues, $newvalues, '<cs_name>');

		header('Location: ' .base_url() . 'index.php/<cs_name>');		
	
	}
	
	function delete(){
		$this->load->helper('url');	
		include APPPATH . "fieldspecs/<cs_name>_spec.php";	
		$fs = new CF_FieldSpec($fieldspec);
		//$oldvalues = CF_ParamReciever::GetReqParamsByList($ar_add_columns,'OV_',false);
		$ar_key_columns = $fs->GetColumnsArray('key');
		$keyvalues = CF_ParamReciever::GetReqParamsByList($ar_key_columns,'',false);

		
		$result = <cs_name>_mdl::do_delete($keyvalues,'<main_table>');
		header('Location: ' .base_url() . 'index.php/<cs_name>');		
			
			
		
	}
	
	
	function update(){
	
		$newvalues = CF_ParamReciever::GetSingleValue('field','value');
		
		$keyvalues = CF_ParamReciever::GetReqParamsPacked('ks', 'kv', ',',true);

		$result = <cs_name>_mdl::do_update($keyvalues, $newvalues, '<cs_name>');

		print json_encode($result);	
	

	}
	
	function index(){
		$this->load->helper('url');		

		include APPPATH . "fieldspecs/<cs_name>_spec.php";	
		$fs = new CF_FieldSpec($fieldspec);

		$filters =  CF_FilterReciever::get_filters($fieldspec);	

		$set =  CF_FilterReciever::filters_to_set($filters);

		// order stuff
		$order_column = $_REQUEST['_order'];
		$order_dir = $_REQUEST['_dir'];
		if(empty($order_dir)){
			$order_dir = 'ASC';
		}
		if(!empty($order_column)){
			$order = array($order_column=>$order_dir);
		}

		
		$records = <cs_name>_mdl::get_records($filters, $order);
	//print_r($fieldspec);	
		$ar_columns = $fs->GetColumnsArray('listquery');
		$ar_columns = array_merge($ar_columns,$fs->GetColumnsArray('list'));

		$ar_lookuplists = <cs_name>_mdl::get_lookuplists($fs,$ar_columns,$set);

		$data['records'] = $records;
		$data['fieldspec'] = $fieldspec;
		$data['ar_lookuplists'] = $ar_lookuplists;
		$data['filters'] = $filters;
		
		$this->load->view('<cs_name>_view', $data);
	}	
	

}